package br.com.fafica.sisape.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;

import br.com.fafica.sisape.conexao.ConnectionFactory;
import br.com.fafica.sisape.control.ControllerTelas;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.SQLException;

public class TelaDadosDoBanco extends JFrame {

	private JPanel contentPane;
	private JTextField txtEndereco;
	private JTextField txtUsuario;
	private JTextField txtSenha;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaDadosDoBanco frame = new TelaDadosDoBanco();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TelaDadosDoBanco() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 273);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblEndereoDoBanco = new JLabel("Endere\u00E7o do Banco:");
		lblEndereoDoBanco.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEndereoDoBanco.setForeground(new Color(0, 0, 128));
		lblEndereoDoBanco.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		lblEndereoDoBanco.setBounds(49, 57, 134, 19);
		contentPane.add(lblEndereoDoBanco);
		
		JLabel lblUsurio = new JLabel("Usu\u00E1rio:");
		lblUsurio.setHorizontalAlignment(SwingConstants.RIGHT);
		lblUsurio.setForeground(new Color(0, 0, 128));
		lblUsurio.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		lblUsurio.setBounds(113, 93, 70, 19);
		contentPane.add(lblUsurio);
		
		JLabel lblSenha = new JLabel("Senha:");
		lblSenha.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSenha.setForeground(new Color(0, 0, 128));
		lblSenha.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		lblSenha.setBounds(113, 129, 70, 19);
		contentPane.add(lblSenha);
		
		txtEndereco = new JTextField();
		txtEndereco.setText("localhost:3306");
		txtEndereco.setForeground(new Color(0, 0, 128));
		txtEndereco.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		txtEndereco.setBounds(193, 54, 151, 25);
		contentPane.add(txtEndereco);
		txtEndereco.setColumns(10);
		
		txtUsuario = new JTextField();
		txtUsuario.setText("root");
		txtUsuario.setForeground(new Color(0, 0, 128));
		txtUsuario.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		txtUsuario.setColumns(10);
		txtUsuario.setBounds(193, 90, 151, 25);
		contentPane.add(txtUsuario);
		
		txtSenha = new JTextField();
		txtSenha.setText("147852");
		txtSenha.setForeground(new Color(0, 0, 128));
		txtSenha.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		txtSenha.setColumns(10);
		txtSenha.setBounds(193, 126, 151, 25);
		contentPane.add(txtSenha);
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ConnectionFactory dados = new ConnectionFactory();
				dados.endereco = txtEndereco.getText();
				dados.usuario = txtUsuario.getText();
				dados.senha = txtSenha.getText();
				Connection con = new ConnectionFactory().getConnection();
				try {
					con.close();
					ControllerTelas control = new ControllerTelas();
					dispose();
				} catch (SQLException e) {
					TelaMensagemErroConexao telaMensagem = new TelaMensagemErroConexao();
					telaMensagem.setLocationRelativeTo(null);
					telaMensagem.setVisible(true);
					e.printStackTrace();
				
				} catch (NullPointerException e) {
					TelaMensagemErroConexao telaMensagem = new TelaMensagemErroConexao();
					telaMensagem.setLocationRelativeTo(null);
					telaMensagem.setVisible(true);
					e.printStackTrace();
				}
			}
		});
		btnOk.setForeground(new Color(0, 0, 128));
		btnOk.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		btnOk.setBounds(193, 189, 89, 32);
		contentPane.add(btnOk);
		
		JLabel lblMysql = new JLabel("MySql");
		lblMysql.setForeground(new Color(0, 0, 128));
		lblMysql.setFont(new Font("Times New Roman", Font.PLAIN, 24));
		lblMysql.setBounds(10, 11, 70, 35);
		contentPane.add(lblMysql);
	}
}
